package com.polaris.live.dialog

import android.content.Context
import android.graphics.Color
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import com.blankj.utilcode.util.ColorUtils
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.databinding.DialogMyBeansInstructionBinding
import com.polaris.live.translation.R.string

/**
 * MyBeansInstructionDialog
 *
 * @author Created by 半仙 on 2024/4/12/012 14:12
 */
class MyBeansInstructionDialog(context: Context) : CenterPopupView(context) {

    private lateinit var mBinding: DialogMyBeansInstructionBinding

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_my_beans_instruction
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogMyBeansInstructionBinding.bind(popupImplView)
        val incomeMessage = context.getString(string.withdraw_income_tip)
        val incomeBuilder = SpannableStringBuilder(incomeMessage)
        val income = context.getString(string.withdraw_income)
        val incomeIndex = incomeMessage.indexOf(income)
        if (incomeIndex != -1) {
            incomeBuilder.setSpan(
                clickableSpan,
                incomeIndex,
                incomeIndex + income.length,
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE
            )
        }
        mBinding.tvIncome.apply {
            highlightColor = ColorUtils.getColor(R.color.color_767678)
            movementMethod = LinkMovementMethod.getInstance()
            text = incomeBuilder
        }

        val rewardMessage = context.getString(string.withdraw_reward_tip)
        val rewardBuilder = SpannableStringBuilder(rewardMessage)
        val reward = context.getString(string.withdraw_reward)
        val rewardIndex = rewardMessage.indexOf(reward)
        if (rewardIndex != -1) {
            rewardBuilder.setSpan(
                clickableSpan,
                rewardIndex,
                rewardIndex + reward.length,
                Spannable.SPAN_INCLUSIVE_EXCLUSIVE
            )
        }
        mBinding.tvReward.apply {
            highlightColor = ColorUtils.getColor(R.color.color_767678)
            movementMethod = LinkMovementMethod.getInstance()
            text = rewardBuilder
        }
    }

    private var clickableSpan: ClickableSpan = object : ClickableSpan() {
        override fun onClick(widget: View) {

        }

        override fun updateDrawState(paint: TextPaint) {
            paint.color = ColorUtils.getColor(R.color.color_1a1a1a)
            paint.bgColor = Color.TRANSPARENT
        }
    }

}